from pygame import image, Surface
from test.load_tiles import load_tiles, get_tile_rect, SIZE  # 改成从第一次作业导入
from test.generate_maze import create_maze  # 改成从第二次作业导入


def parse_grid(data):#strip是函数名,要加()才行
    return [list(row) for row in data.strip().split("\n")]



def draw_grid(data, tile_img, tiles):
    xsize = len(data[0]) * SIZE
    ysize = len(data) * SIZE
    img = Surface((xsize, ysize))
    for y, row in enumerate(data):
        for x, char in enumerate(row):
            rect = get_tile_rect(x, y)
            img.blit(tile_img, rect, tiles[char])
    return img


if __name__ == '__main__':
    tile_img, tiles = load_tiles()
    level = create_maze(12, 7)
    level = parse_grid(level)
    maze = draw_grid(level, tile_img, tiles)
    image.save(maze, 'maze.png')
